package expression.v1.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import expression.v1.config.ExpConfig;
import expression.v1.jsondata.Pagination;
import expression.v1.model.ExpImage;
import expression.v1.model.ExpPkg;
import expression.v1.service.IExpImageService;
import expression.v1.service.IExpPackageService;

@Controller
@RequestMapping("/expMng/")
public class ExpressionMngController {

	@Autowired
	ExpConfig expConfig;

	@Resource
	private IExpPackageService expPkgService;
	@Resource
	private IExpImageService expImgService;

	@RequestMapping("latest.do")
	public String latest() {
		return "expmng/showlatest";
	}
	
	@RequestMapping("listDetail.do")
	public String listDetail(@RequestParam(required = true) String keyword) {
		return "expmng/listDetail";
	}

	@ResponseBody
	@RequestMapping("latestList.do")
	public Map<String, Object> latestList(HttpServletRequest request,
			@RequestParam(required = true) int pageSize,
			@RequestParam(required = true) int pageNow) {
		int from = pageNow * pageSize;
		int limit = from + pageSize;
		List<ExpPkg> expPkgs = expPkgService.queryLatest(from, limit);
		int total = expPkgService.countAll();
		Pagination pagination = new Pagination(pageSize, limit / pageSize,
				total);
		Map<String, Object> retMap = new HashMap<String, Object>();
		retMap.put("pagination", pagination);
		retMap.put("expPkgs", expPkgs);
		return retMap;
	}

	@ResponseBody
	@RequestMapping("detail.do")
	public Map<String, Object> detail(HttpServletRequest request,
			@RequestParam(required = true) int pageSize,
			@RequestParam(required = true) int pageNow,
			@RequestParam(required = true) String keyword) {
		int from = pageNow * pageSize;
		int limit = from + pageSize;
		List<ExpImage> expPkgs = expImgService.queryImgsByPkgKeyword(keyword,
				from, limit);
		int total = expImgService.countByConditions(keyword);
		Pagination pagination = new Pagination(pageSize, limit / pageSize,
				total);
		Map<String, Object> retMap = new HashMap<String, Object>();
		retMap.put("pagination", pagination);
		retMap.put("pkgImgs", expPkgs);
		return retMap;
	}

	@RequestMapping("keywordset.do")
	public String keywordSet() {
		return "expmng/keywordsetting";
	}
	
	@ResponseBody
	@RequestMapping("setting.do")
	public Map<String, Object> setting() {
		int state = 1;
		List<ExpPkg> checkedList = expPkgService.queryCheckedSelKey(state);
		List<ExpPkg> checkingList = expPkgService.queryCheckingSelKey(state);
		Map<String, Object> retMap = new HashMap<String, Object>();
		retMap.put("checked", checkedList);
		retMap.put("checking", checkingList);
		return retMap;
	}
	
	@ResponseBody
	@RequestMapping("set.do")
	public String set(
			@RequestParam(required = true) int state,
			@RequestParam(required = true) String keywords) {
		String[] keywordArray = keywords.split(",");
		String retStr = null;
		int effectsNum = expPkgService.updateQueryKeyState(state, keywordArray);
		retStr = effectsNum == 0 ? "fail" : "success";
		return retStr;
	}
}
